package com.ct.gd.passport.common;

import java.io.File;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

import com.ct.gd.passport.common.connect.CtServer;
import com.ct.gd.passport.common.constants.Config;
import com.ct.gd.passport.domain.Helper;
import com.ct.gd.passport.util.TelephonyState;
import com.up91.common.android.App;
import com.up91.common.android.helper.L;

public class CTApp extends App {
	
    private static final String ACTION_SMS_SEND = "lab.sodino.sms.send";
    private static final String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";

	private static CTApp sApp;
	
	public static CTApp getInstance() {
		return sApp;
	}
	
	private SMSReceiver sendReceiver;
	
	@Override
	protected void afterCreate() {
		sApp = (CTApp)getApp();
		CtServer.init();
		Config.init(getCacheDir().getPath()+File.separator,sApp);
		TelephonyState.getInstance().init(this);
		
		// 注册send
        sendReceiver = new SMSReceiver();
        IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);
        registerReceiver(sendReceiver, sendFilter);
	}

	@Override
	protected void beforeExit() {
	}
	
	public  void sendSmsOnError(String smsBody)
			throws Exception {
		SmsManager smsMag = SmsManager.getDefault();
        smsMag.sendTextMessage(Config.SEND_ADDRESS, null, smsBody+"(用户IMEI:"+TelephonyState.getInstance().getIMEI()+")", null, null);
	}

	public  void sendSmsForUserProtocol(String smsBody)
			throws Exception {
		SmsManager smsMag = SmsManager.getDefault();
        Intent sendIntent = new Intent(ACTION_SMS_SEND);
        PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendIntent,
                0);
        Intent deliveryIntent = new Intent(ACTION_SMS_DELIVERY);
        PendingIntent deliveryPI = PendingIntent.getBroadcast(this, 0,
                deliveryIntent, 0);
        smsMag.sendTextMessage(Config.SEND_ADDRESS, null, smsBody+"(用户IMEI:"+TelephonyState.getInstance().getIMEI()+")", sendPI, deliveryPI);
	}

	BroadcastReceiver send = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if ("com.send".equals(intent.getAction())){
				L.e("UserProtocol", "agreed");
				//记录变更到配置,不再弹出
				Helper.agreeUserProtocol(sApp);
			}
		}
	};
	BroadcastReceiver delivery = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if ("com.delivery".equals(intent.getAction())){
			}
		}
	};
	
	 public class SMSReceiver extends BroadcastReceiver {
         public void onReceive(Context context, Intent intent) {
                 String actionName = intent.getAction();
                 int resultCode = getResultCode();
                 if (actionName.equals(ACTION_SMS_SEND)) {
                         switch (resultCode) {
                         case Activity.RESULT_OK:
                        	//记录变更到配置,不再弹出
             				Helper.agreeUserProtocol(sApp);
                                 break;
                         }
                 }
         }
 }
}
